package llama.view.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.text.DefaultCaret;

import llama.core.IUser;
import llama.ctrl.ControllerFactory;
import llama.ctrl.SendMessageCtrl;

public class ChatInputPanel extends JPanel {
	private static final int MIN_CHAT_INPUT_ROWS = 2;
	private static final int MAX_CHAT_INPUT_ROWS = 6;
	
	private JTextArea textArea;
	private JScrollPane scrollPane;
	private IUser toUser;
	
	/**
	 * Create the panel.
	 */
	public ChatInputPanel() {
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setLayout(new BorderLayout(0, 0));
		
		scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		textArea = new JTextArea();
		textArea.setRows(MIN_CHAT_INPUT_ROWS);
		textArea.addKeyListener(new SendMessageKeyListener());
		textArea.setBorder(BorderFactory.createEtchedBorder());
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);

		DefaultCaret caret = (DefaultCaret)textArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		
		scrollPane.setViewportView(textArea);
	}
	
	public void setReceiver(IUser toUser) {
		this.toUser = toUser;
	}
	
	public IUser getReceiver() {
		return toUser;
	}
	
	private void revalidateInputHeight() {
		if(textArea.getLineCount() < MIN_CHAT_INPUT_ROWS) {
			textArea.setRows(MIN_CHAT_INPUT_ROWS);
		}
		
		if(textArea.getLineCount() <= MAX_CHAT_INPUT_ROWS) {
			textArea.setPreferredSize(new Dimension(textArea.getPreferredSize().width, (textArea.getFont().getSize()+5)*textArea.getLineCount())); //TODO something to remove QD
			scrollPane.getViewport().setPreferredSize(textArea.getPreferredSize());
			scrollPane.setPreferredSize(textArea.getPreferredSize());
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		} else {
			textArea.setPreferredSize(new Dimension(textArea.getPreferredSize().width, (textArea.getFont().getSize()+5)*textArea.getLineCount()));
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		}
		
		revalidate();
		repaint();
	}
	
	/**
	 * @pre getReceiver() != null
	 */
	private void sendMessage() {
		if((textArea.getText() != null) && !(textArea.getText().isEmpty())) {
			SendMessageCtrl sendMessageCtrl = (SendMessageCtrl) ControllerFactory.getController(SendMessageCtrl.class);
			sendMessageCtrl.setMessage(textArea.getText(), toUser);
			sendMessageCtrl.execute();
		}
		
	}
	
	private class SendMessageKeyListener extends KeyAdapter {
		@Override
		public void keyPressed(KeyEvent arg0) {
			if(arg0.getKeyCode()==KeyEvent.VK_ENTER) {
				if(arg0.isShiftDown()) {
					textArea.append("\n");
				} else {
					sendMessage();
					textArea.setText("");
					textArea.setCaretColor(Color.white); //Visual tweak
				}
			}
			
			revalidateInputHeight();
		}
		@Override
		public void keyReleased(KeyEvent arg0) {
			if(arg0.getKeyCode()==KeyEvent.VK_ENTER 
					&& !arg0.isShiftDown()
					&& textArea.getText().equals("\n")) {
				textArea.setText("");
				textArea.setCaretColor(Color.black); //Visual tweak
			}
			
			revalidateInputHeight();
		}
	}

	public void setFocusToInput() {
		textArea.requestFocusInWindow();
	}

}
